#-------------------------------------------------------------------------------
#!	\file		convert.awk
#!
#!	\brief		Convert types in a C header file.
#!
#!	\details
#!				This awk script converts all the Synopsys / Denali created types
#!				to include the file name.  This is done to effectively create a
#!				name space for each peripheral.
#!
#!				The goal is to eliminate all type collisions between peripherals.
#!
#!	\author		ACV
#!
#!	\version	1
#!
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#	Before the file is processed:
#	Create the new file name from the c header
#	Put an additional header on the file
#-------------------------------------------------------------------------------
BEGIN {
	split(ARGV[1],in_name,".")
	out_name = in_name[1] ".h.t"
	name_space = in_name[1]
	u_type = name_space "_t"

	printf("//-------------------------------------------------------------------\n") > out_name
	printf("// header file converter.\n") > out_name
	printf("// converted from: %s\n",ARGV[1]) > out_name
	printf("// Generation Date: %s\n",strftime()) > out_name
	printf("//\n") > out_name
	printf("// This is a generated file.  It is created by the convert.awk awk script.\n") > out_name
	printf("//\n") > out_name
	printf("// Do not edit.\n") > out_name
	printf("//-------------------------------------------------------------------\n") > out_name
}

#-------------------------------------------------------------------------------
#	Do not act on comments
#-------------------------------------------------------------------------------
$1 ~/^\/\// {
	print $0 > out_name
	next
}

#-------------------------------------------------------------------------------
#	Find all types for replacement
#-------------------------------------------------------------------------------
$0 ~/[A-Za-z0-9_]+_t/ {
	match($0, /[A-Za-z0-9_]+_t/)
	s_type = substr($0,RSTART,RLENGTH)
	if ((s_type !~ /uint[0-9]+_t/) && (s_type !~ u_type)) {
		n_type = name_space "_" s_type
		sub(/[A-Za-z0-9_]+_t/,n_type)
	}
	print $0 > out_name
	next
}

#-------------------------------------------------------------------------------
#	Default rule: output the line
#-------------------------------------------------------------------------------
{
	print $0 > out_name
	next
}
#-------------------------------------------------------------------------------
#	At the end of the file:
#	Close the output file
#-------------------------------------------------------------------------------
END {
	close(out_name)
}
